<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onAfterDisplayComponentHelpDesk', 'plgHelpDeskFeedbackHistory_onAfterDisplayComponentHelpDesk' );
$_MAMBOTS->registerFunction( 'showHistory', 'plgHelpDeskFeedbackHistory_showHistory' );

	/**
	 * Executes the plugin
	 * @return 
	 */
	function plgHelpDeskFeedbackHistory_onAfterDisplayComponentHelpDesk() {
		$return = plgHelpDeskFeedbackHistory::onAfterDisplayComponentHelpDesk();
		return $return;
	}
	
	/**
	 * Executes the plugin
	 * @return 
	 */
	function plgHelpDeskFeedbackHistory_showHistory( $element ) {
		$return = plgHelpDeskFeedbackHistory::showHistory( $element );
		return $return;
	}


/**
 * HelpDesk Plugin
 *
 */
class plgHelpDeskFeedbackHistory 
{
	/**
	 * Checks to make sure that this plugin is the one being called by the component
	 *
	 * @access public
	 * @return mixed Parameter value
	 * @since 1.5
	 */
	function _isMe( $name ) 
	{
		$success = false;
		if ($name == 'feedbackhistory' ) {
			$success = true;
		}
		return $success;
	}
	
	/**
	 * Gets a parameter value
	 *
	 * @access public
	 * @return mixed Parameter value
	 * @since 1.5
	 */
	function _getParameter( $name, $default=null ) 
	{
		static $params;
		
		if (!is_object($params))
		{
			$database = HelpDeskConfig::getDBO();
			$folder = strtolower( 'HelpDesk' );
			$element = strtolower( 'feedbackhistory' );
			
			$return = "";
	
			$query = "
				SELECT 
					`params`
				FROM 
					#__mambots
				WHERE 
					`element` = '{$element}'
				AND 
					`folder` = '{$folder}'
			";
			$database->setQuery( $query );
			$mambot = $database->loadObject();
			
			$params = new mosParameters( $mambot->params );			
		}
		
		$return = $params->get( $name, $default );
		return $return;
	}
	
	/**
	 * Method is called 
	 * before displaying a comment.
	 * Note: $comment->authorimage is available, and is already set to the default
	 * 
	 * @return 
	 * @param $row Object
	 * @param $body Object
	 * @param $user Object
	 * @param $args Object
	 */
	function onAfterDisplayComponentHelpDesk() 
	{
		echo "<p><center>";
			echo plgHelpDeskFeedbackHistory::_displaySummary();
		echo "</center></p>";
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function showHistory( $element )
	{
		$success = false;
		if (!$isMe = plgHelpDeskFeedbackHistory::_isMe($element)) 
		{
			return $success;
		}
		
		$user = HelpDeskConfig::getUser();
		$html = "";	
		$html .= "<link href='".HelpDeskUrl::root()."components/com_helpdesk/includes/css/helpdesk.css' rel='stylesheet' type='text/css' />";

		$stats = plgHelpDeskFeedbackHistory::getStats();
		$headerText = plgHelpDeskFeedbackHistory::_getParameter( 'headertext', HelpDeskText::_("Our Feedback History") );

		$html .= "<center><h4>{$headerText}</h4></center>";
		$html .= "<table class='userlist'>";
		$html .= "<tr>";
			$html .= "<th style='text-align: center;'>".HelpDeskText::_("Rating")."</th>";
			$html .= "<th style='text-align: center;'>".HelpDeskText::_("Tickets")."</th>";
		$html .= "</tr>";
		$html .= "<tr>";
			// # w/ 5 stars
			$html .= "<td style='text-align: center;'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( '5' )."</td>";
			$html .= "<td style='text-align: center;'>".$stats->five->count."</td>";
		$html .= "</tr>";
		$html .= "<tr>";
		// TODO # w/ 4 stars
			$html .= "<td style='text-align: center;'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( '4' )."</td>";
			$html .= "<td style='text-align: center;'>".$stats->four->count."</td>";
		$html .= "</tr>";
		$html .= "<tr>";
		// TODO # w/ 3 stars
			$html .= "<td style='text-align: center;'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( '3' )."</td>";
			$html .= "<td style='text-align: center;'>".$stats->three->count."</td>";
		$html .= "</tr>";
		$html .= "<tr>";
		// TODO # w/ 2 stars
			$html .= "<td style='text-align: center;'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( '2' )."</td>";
			$html .= "<td style='text-align: center;'>".$stats->two->count."</td>";
		$html .= "</tr>";
		$html .= "<tr>";
		// TODO # w/ 1 stars
			$html .= "<td style='text-align: center;'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( '1' )."</td>";
			$html .= "<td style='text-align: center;'>".$stats->one->count."</td>";
		$html .= "</tr>";
		$html .= "<tr>";
			$html .= "<th style='text-align: center;'>".HelpDeskText::_("Total")."</th>";
			$html .= "<th style='text-align: center;'>".$stats->overall->count."</th>";
		$html .= "</tr>";
		$html .= "</table>";
      
		return $html;	
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function _displaySummary()
	{
		$html = "";
		$stats = plgHelpDeskFeedbackHistory::getStats();

		HelpDeskHTML::behaviorModal( 'a.modal');
		$url = "index2.php?option=com_helpdesk&task=doTask&element=feedbackhistory&elementTask=showHistory";

		// TODO Param: Display overall header? 
		$text = HelpDeskText::_( 'Our overall feedback rating is' );
		// $html .= HelpDeskUrl::popup( $url, $text, '500', '500' );
		$html .= $text;
		$html .= "<br/>";
				
		// TODO Param: Display image? 
		$num = 0;
		if ($stats->overall->count > '0') { $num = $stats->overall->sum / $stats->overall->count; }
		$html .= HelpDeskHelperHelpDesk::getFeedbackRatingImage ( $num );
		$html .= "<br/>";
		
		// TODO Param Display Infolink?		
		$text = HelpDeskText::_( 'Find out what this means' );
		$html .= HelpDeskUrl::popup( $url, $text, '500', '325' );
		
		return $html;	
	}
	
	/**
	 * 
	 * @param $refresh
	 * @return unknown_type
	 */
	function getStats( $refresh='0' ) 
	{
		static $instance;
		
		if (!is_object($instance) || $refresh == '1' )
		{
			$instance = new HelpDeskObject();
			$instance->overall 	= plgHelpDeskFeedbackHistory::getOverall();
			$instance->five		= plgHelpDeskFeedbackHistory::getByRating( '5' );
			$instance->four		= plgHelpDeskFeedbackHistory::getByRating( '4' );
			$instance->three	= plgHelpDeskFeedbackHistory::getByRating( '3' );
			$instance->two		= plgHelpDeskFeedbackHistory::getByRating( '2' );
			$instance->one		= plgHelpDeskFeedbackHistory::getByRating( '1' );
		}

		return $instance;
	}
	
	/**
	 * 
	 * @param $refresh
	 * @return unknown_type
	 */
	function getOverall( $refresh='0' ) 
	{
		static $instance;
		
		if (!is_object($instance) || $refresh == '1' )
		{
			$database = HelpDeskConfig::getDBO();
						
			$query = " 
				SELECT 
					SUM(t.feedback_rating) AS sum, COUNT(t.id) AS count
				FROM
					#__helpdesk_tickets AS t
				WHERE 1
					AND ( t.status = '2' ) AND ( t.firstresponse_by > '0' )
					AND t.feedback_by > '0'
			";
			$database->setQuery( $query );
			$instance = $database->loadObject();
		}

		return $instance;
	}
	
	/**
	 * 
	 * @param $refresh
	 * @return unknown_type
	 */
	function getByRating( $rating, $refresh='0' ) 
	{
		static $instance;
		$rating = intval($rating);
		
		if (!is_array($instance) || !isset($instance[$rating]) || $refresh == '1' )
		{
			if (!is_array($instance)) { $instance = array(); }
			$database = HelpDeskConfig::getDBO();
			
			$query = " 
				SELECT 
					SUM(t.feedback_rating) AS sum, COUNT(t.id) AS count
				FROM
					#__helpdesk_tickets AS t
				WHERE 1
					AND ( t.status = '2' ) AND ( t.firstresponse_by > '0' )
					AND t.feedback_by > '0'
					AND t.feedback_rating = '{$rating}'
			";
			$database->setQuery( $query );
			$instance[$rating] = $database->loadObject();
		}

		return $instance[$rating];
	}

}